package com.practice.tkmybatis.controller;

import com.practice.tkmybatis.dao.UserEntity;
import com.practice.tkmybatis.mapper.UserMapper;
import com.practice.tkmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description : 测试tkmybatis功能
 * @Author : asus
 * @Version : xxx
 * @Date : 2021/6/8 22:38
 */
@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    public UserEntity findById(int id) {
        UserEntity entity = userService.findById(id);
        System.out.println(entity);
        return entity;
    }

    public void insertUser() {
        UserEntity userEntity = new UserEntity();
        userEntity.setUseName("李思").setAge(18).setAddress("南京").setSex(1).setPhone(null);
        userService.insert(userEntity);
    }

    public UserEntity findByExample() {
        Example example = new Example(UserEntity.class);
        example.createCriteria().andEqualTo("useName", "张三");
        List<UserEntity> userEntities = userMapper.selectByExample(example);
        return userEntities.get(0);
    }
}
